package com.tianxiao.faas.application.web.corner;

import com.google.common.collect.Lists;
import com.tianxiao.faas.api.param.corner.BannerPageParam;
import com.tianxiao.faas.api.param.corner.BannerSaveParam;
import com.tianxiao.faas.api.param.corner.BasePageParam;
import com.tianxiao.faas.common.access.Access;
import com.tianxiao.faas.common.result.Result;
import com.tianxiao.faas.common.util.DateUtils;
import com.tianxiao.faas.common.util.LikeQueryUtils;
import com.tianxiao.faas.common.util.PageUtils;
import com.tianxiao.faas.mapper.dao.BannerModelMapper;
import com.tianxiao.faas.mapper.model.BannerModel;
import com.tianxiao.faas.mapper.model.BannerModelExample;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/admin/banner/")
public class BannerAdminController {
    @Resource
    private BannerModelMapper bannerModelMapper;

    @RequestMapping(value = "/save")
    @Access(access = {"admin"}, action = "banner修改/插入")
    public Result<Boolean> save(@RequestBody BannerSaveParam param) {
        BannerModel record = new BannerModel();
        long currentTimeMillis = System.currentTimeMillis();
        record.setDeleted(0);
        String endTime = param.getEndTime();
        record.setEndTime(DateUtils.format(endTime));
        record.setId(param.getId());
        record.setStartTime(DateUtils.format(param.getBeginTime()));
        record.setImg(param.getUrl());
        record.setName(param.getName());
        record.setLink(param.getObjectId());
        record.setType(param.getType());
        record.setUpdated(currentTimeMillis);
        if (record.getId() == null || record.getId() <= 0) {
            record.setCreated(currentTimeMillis);
            bannerModelMapper.insertSelective(record);
        } else {
            bannerModelMapper.updateByPrimaryKeySelective(record);
        }
        return Result.builder().data(true).success(true);
    }

    @RequestMapping(value = "/unShelve")
    @Access(access = {"admin"}, action = "下架banner")
    public Result<Boolean> unshelve(Long id) {
        BannerModel bannerModel = bannerModelMapper.selectByPrimaryKey(id);
        bannerModel.setDeleted(1);
        bannerModelMapper.updateByPrimaryKeySelective(bannerModel);
        return Result.builder().data(true).success(true);
    }

    @RequestMapping(value = "/delete")
    @Access(access = {"admin"}, action = "删除")
    public Result<Boolean> delete(@RequestParam("id") Long id) {
        BannerModel bannerModel = bannerModelMapper.selectByPrimaryKey(id);
        bannerModel.setDeleted(2);
        bannerModelMapper.updateByPrimaryKeySelective(bannerModel);
        return Result.builder().data(true).success(true);
    }

    @RequestMapping(value = "/list")
    @Access(access = {"admin"})
    public Result<List<BannerSaveParam>> list(@RequestBody BannerPageParam param) {
        Integer pageSize = param.getPageSize();
        Integer page = param.getPage();
        String key = param.getKey();
        Integer status = param.getStatus();
        Long start = DateUtils.format(param.getStartTime());
        Long endTime = DateUtils.format(param.getEndTime());
        // 未开始，开始时间大于当前时间
        key = LikeQueryUtils.getKey(key);
        start = System.currentTimeMillis();
        List<BannerModel> page1 = bannerModelMapper.page(key, start,
                endTime,status, PageUtils.getOffset(page, pageSize), pageSize);
        if (CollectionUtils.isEmpty(page1)) {
            return Result.builder().data(Lists.newArrayList()).success(true);
        }
        List<BannerSaveParam> result = Lists.newArrayList();
        BannerSaveParam e = null;
        for (BannerModel bannerModel : page1) {
            e = new BannerSaveParam();
            e.setBeginTime(DateUtils.format(bannerModel.getStartTime()));
            e.setEndTime(DateUtils.format(bannerModel.getEndTime()));
            e.setId(bannerModel.getId());
            e.setName(bannerModel.getName());
            e.setObjectId(bannerModel.getLink());
            e.setUrl(bannerModel.getImg());
            e.setCreateTime(DateUtils.format(bannerModel.getCreated()));
            e.setEditTime(DateUtils.format(bannerModel.getUpdated()));
            e.setStatus(bannerModel.getDeleted());
            e.setType(bannerModel.getType());
            result.add(e);
        }
        long l = bannerModelMapper.count(key, start,
                endTime,status);

        return Result.builder().data(result).pageSize(pageSize).pageNo(page).totalCount((int)l)
                .totalPage().success(true);
    }
}
